package com.crm.redis.service.returnMoneyManager.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.crm.common.util.DateUtil;
import com.crm.model.vo.returnMoney.ReturnMoneyRedisVO;
import com.crm.redis.RedisPrefix;
import com.crm.redis.mapper.ValueRMapper;
import com.crm.redis.service.returnMoneyManager.ReturnMoneyRedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/8/12 9:49
 */
@Service
public class ReturnMoneyRedisServiceImpl implements ReturnMoneyRedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 业绩、到款金额存redis
     * @param companyId
     * @param gradeMoney
     * @param returnMoney
     * @param arriveTime
     */
    @Override
    public void returnMoneyToRedis(String companyId, BigDecimal gradeMoney, BigDecimal returnMoney, Date arriveTime) {
        if (Objects.isNull(returnMoney)){
            returnMoney = new BigDecimal("0");
        }
        if (Objects.isNull(gradeMoney)){
            gradeMoney = new BigDecimal("0");
        }
        if (Objects.nonNull(arriveTime) && StringUtils.isNotBlank(companyId)){
            Integer arriveDate = Integer.valueOf(DateUtil.formatDateTime(arriveTime).substring(0,10).replaceAll("-",""));
            String year = arriveDate.toString().substring(0,4);
            String key = RedisPrefix.RETURN_MONEY_AND_GRADE_MOENY + companyId+":"+year;
            String jsonDate = ValueRMapper.getRedisString(redisTemplate, key);
            List<ReturnMoneyRedisVO> list = JSONArray.parseArray(jsonDate, ReturnMoneyRedisVO.class);
            if (Objects.nonNull(list)){
                for (int i=list.size()-1;i > -1;i--){
                    if (arriveDate.toString().equals(list.get(i).getDate().toString())){
                        list.get(i).setGradeMoney(list.get(i).getGradeMoney().add(gradeMoney));
                        list.get(i).setReturnMoney(list.get(i).getReturnMoney().add(returnMoney));
                        ValueRMapper.setRedisString(redisTemplate, key, JSON.toJSONString(list));
                        return;
                    }
                }
            }
            if (Objects.isNull(list)){
                list = new ArrayList<>();
            }
            ReturnMoneyRedisVO vo = new ReturnMoneyRedisVO();
            vo.setDate(arriveDate);
            vo.setGradeMoney(gradeMoney);
            vo.setReturnMoney(returnMoney);
            list.add(vo);
            ValueRMapper.setRedisString(redisTemplate, key,JSON.toJSONString(list));
        }
    }

    /**
     * 修改回款金额。业绩金额
     * @param gradeMoney 业绩金额
     * @param companyId 公司id
     * @param arriveMoney 到款金额
     * @param arriveTime 到款时间
     * @param oldArriveMoney 原来的到款金额
     * @param oldArriveTime 原来的到款时间
     */
    @Override
    public void updateReturnMoneyToRedis(BigDecimal gradeMoney, String companyId, BigDecimal arriveMoney, Date arriveTime, BigDecimal oldArriveMoney, Date oldArriveTime) {
        Integer arriveDate = Integer.valueOf(DateUtil.formatDateTime(arriveTime).substring(0,10).replaceAll("-",""));
        Integer oldArriveDate = Integer.valueOf(DateUtil.formatDateTime(oldArriveTime).substring(0,10).replaceAll("-",""));
        String year = arriveDate.toString().substring(0,4);
        String key = RedisPrefix.RETURN_MONEY_AND_GRADE_MOENY + companyId+":"+year;
        String jsonDate = ValueRMapper.getRedisString(redisTemplate, key);
        List<ReturnMoneyRedisVO> list = JSONArray.parseArray(jsonDate, ReturnMoneyRedisVO.class);
        if (Objects.nonNull(list)){
            for (int i=list.size()-1;i > -1;i--){
                //减原来的金额
                if (oldArriveDate.toString().equals(list.get(i).getDate().toString())){
                    list.get(i).setGradeMoney(list.get(i).getGradeMoney().subtract(gradeMoney));
                    list.get(i).setReturnMoney(list.get(i).getReturnMoney().subtract(oldArriveMoney));
                    ValueRMapper.setRedisString(redisTemplate, key, JSON.toJSONString(list));
                }
            }
            returnMoneyToRedis(companyId,gradeMoney,arriveMoney,arriveTime);
        }
    }

    /**
     * 删除业绩金额提成金额
     * @param arriveTime 到款时间
     * @param companyId 公司id
     * @param arriveMoney 金额
     * @param gradeMoney 业绩
     */
    @Override
    public void delReturnMoney(Date arriveTime, String companyId, BigDecimal arriveMoney, BigDecimal gradeMoney) {
        Integer arriveDate = Integer.valueOf(DateUtil.formatDateTime(arriveTime).substring(0,10).replaceAll("-",""));
        String year = arriveDate.toString().substring(0,4);
        String key = RedisPrefix.RETURN_MONEY_AND_GRADE_MOENY + companyId+":"+year;
        String jsonDate = ValueRMapper.getRedisString(redisTemplate, key);
        List<ReturnMoneyRedisVO> list = JSONArray.parseArray(jsonDate, ReturnMoneyRedisVO.class);
        if (Objects.nonNull(list)){
            for (int i=list.size()-1;i > -1;i--){
                //减原来的金额
                if (arriveDate.toString().equals(list.get(i).getDate().toString())){
                    list.get(i).setGradeMoney(list.get(i).getGradeMoney().subtract(gradeMoney));
                    list.get(i).setReturnMoney(list.get(i).getReturnMoney().subtract(arriveMoney));
                    ValueRMapper.setRedisString(redisTemplate, key, JSON.toJSONString(list));
                }
            }
        }
    }

    /**
     * 获取回款、业绩List
     * @param companyId
     * @param thisDate
     * @return
     */
    @Override
    public List<ReturnMoneyRedisVO> getRedisReturnMoney(String companyId, Date thisDate) {
        Integer arriveDate = Integer.valueOf(DateUtil.formatDateTime(thisDate).substring(0,10).replaceAll("-",""));
        String year = arriveDate.toString().substring(0,4);
        String key = RedisPrefix.RETURN_MONEY_AND_GRADE_MOENY + companyId+":"+year;
        String jsonDate = ValueRMapper.getRedisString(redisTemplate, key);
        List<ReturnMoneyRedisVO> list = JSONArray.parseArray(jsonDate, ReturnMoneyRedisVO.class);
        return list;
    }
}
